<template>
	<xn-form-container
		:title="formData.id ? '编辑客户类型' : '增加客户类型'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="客户类型：" name="customTypeName">
				<a-input v-model:value="formData.customTypeName" placeholder="请输入客户类型" allow-clear />
			</a-form-item>
			<a-form-item label="类型状态：" name="customTypeStatus">
				<a-radio-group v-model:value="formData.customTypeStatus" placeholder="请选择类型状态" :options="customTypeStatusOptions" />
			</a-form-item>
			<a-form-item label="客户折扣：" name="customTypeDiscount">
				<a-input v-model:value="formData.customTypeDiscount" placeholder="请输入客户折扣" allow-clear />
			</a-form-item>
			<a-form-item label="适用价格：" name="customTypePrice">
				<a-input v-model:value="formData.customTypePrice" placeholder="请输入适用价格" allow-clear />
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierCustomTypeForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import supplierCustomTypeApi from '@/api/supplier/custom/supplierCustomTypeApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const customTypeStatusOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		customTypeStatusOptions.value = tool.dictList('COMMON_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		customtypeName: [required('请输入客户类型')],
		customtypeStatus: [required('请输入类型状态')],
		customtypeDiscount: [required('请输入客户折扣')],
		sortCode: [required('请输入排序码')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				supplierCustomTypeApi
					.supplierCustomTypeSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
